package com.example.myscaleimageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
*  @author  tang
*  @date    2015年2月25日
*/
public class ScaleImageView extends View {

	public ScaleImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ScaleImageView(Context context) {
		super(context);
	}

	private Bitmap mBitmap;
	private Rect bitmapRect; // 定义刚好存放图片的矩形
	private Rect disRect; // 填充区域矩形
	private float maxScale;	//设置缩放的最大值
	private float minScale;	//设置缩放的最小值
	private Matrix matrix = new Matrix();

	public void setmBitmap(Bitmap bitmap) {
		this.mBitmap = bitmap;
		// 定义存放图片的矩形大小
		bitmapRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		
	}
	/**
	 * 设置缩放的最大值
	 * @param maxScale
	 */
	public void setMaxScale(float maxScale) {
		this.maxScale = maxScale;
	}
	/**
	 * 设置缩放的最小值
	 * @param minScale
	 */
	public void setMinScale(float minScale) {
		this.minScale = minScale;
	}
	private boolean isFirst = true;
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (isFirst) {
			disRect = new Rect(0,0,getMeasuredWidth(),getMeasuredHeight());
			//计算宽的缩放倍数
			float scaleX = (float)getMeasuredWidth() / mBitmap.getWidth();
			//计算高的缩放倍数
			float scaleY = (float)getMeasuredHeight() / mBitmap.getWidth();
			float scale = Math.min(scaleX, scaleY);
			
		}
	}
	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(mBitmap,matrix , null);
	}
}











